﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KL.Store.Core.Domain;
using TN.Data;
using System.Web;

namespace KL.Store.Data
{
    public class dbProductInfo
    {
        #region Get

        public static ProductInfo Get(int ProductId, short LineNo)
        {
            IList<ProductInfo> objectList = new List<ProductInfo>();
            SqlHelper.FillList(dbParameters.dsnStore, "SPR_ESTORE_ProductInfo_Get", objectList, new object[] { ProductId, LineNo });

            return objectList.FirstOrDefault();
        }

        #endregion

        #region GetList

        public static IList<ProductInfo> GetList(int ProductId)
        {
            IList<ProductInfo> objectList = new List<ProductInfo>();
            SqlHelper.FillList(dbParameters.dsnStore, "SPR_ESTORE_ProductInfo_List", objectList, new object[] { ProductId });

            return objectList;
        }

        #endregion

        #region GetListHtml

        public static string GetListHtml(int ProductId)
        {
            var cacheKey = "GetListHtml" + ProductId;
            if (HttpRuntime.Cache[cacheKey] != null)
            {
                return HttpRuntime.Cache[cacheKey] as string;
            }
            Product p = dbProduct.Get(ProductId, 0);
            IList<ProductInfo> objectList = new List<ProductInfo>();
            SqlHelper.FillList(dbParameters.dsnStore, "SPR_ESTORE_ProductInfo_List", objectList, new object[] { ProductId });
            if (p.UstUrunId != 0 && objectList.Where(o => o.Information != "").Count() <= 0)
            {
                objectList = new List<ProductInfo>();
                SqlHelper.FillList(dbParameters.dsnStore, "SPR_ESTORE_ProductInfo_List", objectList, new object[] { p.UstUrunId });
            }
            var sb = new StringBuilder();
            if (objectList.Count() > 0)
            {
                sb.Append("<br /><table class=\"attribute table table-bordered\">");
                sb.Append("<tr><td colspan=2>Ürün Özellikleri</td></tr>");
            }
            foreach (var info in objectList)
            {
                if (String.IsNullOrEmpty(info.Information))
                {
                    sb.Append("<tr><td colspan=2>" + info.Type + "</td></tr>");
                }
                else
                {
                    sb.Append("<tr><td>" + info.Type + "</td><td>" + info.Information + "</td></tr>");
                }
            }
            if (objectList.Count() > 0)
            {
                sb.Append("</table>");
            }
            HttpRuntime.Cache.Add(cacheKey, sb.ToString(), null, DateTime.UtcNow.AddDays(1), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.NotRemovable, null);
            return sb.ToString();
        }

        #endregion

        #region Insert

        public static void Insert(int ProductId, string Type, string Information)
        {
            SqlHelper.ExecuteNonQuery(dbParameters.dsnStore, "SPR_ESTORE_ProductInfo_Insert", new object[] { ProductId, Type, Information });
        }

        #endregion

        #region InsertDirect

        public static void InsertDirect(int ProductId, string Type, string Information)
        {
            SqlHelper.ExecuteNonQuery(dbParameters.dsnStore, "SPR_ESTORE_ProductInfo_Insert_Direct", new object[] { ProductId, Type, Information });
        }

        #endregion

        #region Update

        public static void Update(int ProductId, short LineNo, short NewLineNo, string Type, string Information)
        {
            SqlHelper.ExecuteNonQuery(dbParameters.dsnStore, "SPR_ESTORE_ProductInfo_Update", new object[] { ProductId, LineNo, NewLineNo, Type, Information });
        }

        #endregion

        #region EditSingle

        public static int EditSingle(int orgsatirno, int datasatirno, string datatype, string datainformation, int dataproduct)
        {
            int id = (int)SqlHelper.ExecuteNonQuery(dbParameters.dsnStore, "SPR_ESTORE_ProductInfo_Edit_Single", new object[] { orgsatirno, datasatirno, datatype, datainformation, dataproduct });
            return id;
        }

        #endregion

        #region Remove

        public static int Remove(int ProductId, int SatirNo)
        {
            int id = (int)SqlHelper.ExecuteNonQuery(dbParameters.dsnStore, "SPR_ESTORE_ProductInfo_Remove", new object[] { ProductId, SatirNo });
            return id;
        }

        #endregion

        #region ProductPropertiesInsertFromMain

        public static int ProductPropertiesInsertFromMain(int MainProductId, int SubProductId)
        {
            int id = (int)SqlHelper.ExecuteScalar(dbParameters.dsnStore, "SPR_ESTORE_Product_Info_MainToSub", new object[] { MainProductId, SubProductId });
            return id;
        }

        #endregion

        #region Ekstra Alan Güncelleme

        public static int UpdateExtraField(string Field, string Value, int ProductId, int SatirNo, string Original)
        {

            int result = (int)SqlHelper.ExecuteScalar(dbParameters.dsnStore, "SPR_ESTORE_Product_EkstraAlan_Guncelle", new object[] { Field, Value, ProductId, SatirNo, Original });
            return result;
        }

        #endregion
    }
}
